﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Money.Interfaces;
using Money.Data;
using One.Web;
using One.Web.UI.WebControls;
using Resources;
using One.Framework;
using One.Framework.Security;

public partial class Admin_ChangePassword : System.Web.UI.Page
{
    private int userId;
    private IUserService userService;
    private User user = null;
    private string redirectUrl = "ChangePassword.aspx";
    private int success;

    protected void Page_Load(object sender, EventArgs e)
    {
        userService = ServiceFactory.Create<IUserService>();

        GetParams();
        AddRules();
        LoadData();
        if (!IsPostBack)
            UpdateView();
    }

    protected void ChangePassword(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            if (user != null)
            {
                if (EncryptUtility.HashMD5(tbOldPassword.Text) == user.Password)
                {
                    if (tbNewPassword.Text == tbConfirmNewPassword.Text)
                    {
                        user.Password = EncryptUtility.HashMD5(tbNewPassword.Text);
                        userService.UpdateUser(user);
                        if (user.UserId == WebSession.CurrentUser.UserId)
                            Response.Redirect("EditMyInfo.aspx");
                        else
                            Response.Redirect("EditUser.aspx?userId=" + user.UserId);
                    }
                    else
                        success = -2;
                }
                else
                {
                    success = -1;
                }
            }
            else
                success = -3;
            Response.Redirect(string.Format("{0}?userId={1}&success={2}", redirectUrl, userId, success));
        }
    }

    private void GetParams()
    {
        userId = QueryStringUtility.GetInt32("userId", 0);
        success = QueryStringUtility.GetInt32("success", 0);
    }

    private void LoadData()
    {
        if (userId != 0)
        {
            try
            {
                user = userService.GetUserById(userId);
            }
            catch
            {
                user = null;
            }
        }
    }

    private void UpdateView()
    {
        pageTitle.Text = Resources.Strings.ChangePassword;
        if (user != null)
        {
            if (user.UserId == WebSession.CurrentUser.UserId)
                pageTitle.Text = Resources.Strings.ChangeMyPassword;
            else
            {
                if (WebSession.CurrentUser.IsInRole(RoleType.User))
                {
                    messageLabel.Visible = true;
                    messageLabel.Text = Resources.Strings.NotHavePermission;
                    btAddOrUpdate.Enabled = false;
                    return;
                }
            }
        }
        else
        {
            btAddOrUpdate.Enabled = false;
        }
        if (success != 0)
        {
            messageLabel.Visible = true;
            messageLabel.Text = string.Empty;
        }
        else
        {
            messageLabel.Visible = false;
        }
        switch (success)
        {
            case -1:
                messageLabel.Text = Strings.OldPasswordNotCorrect;
                break;
            case -2:
                messageLabel.Text = Strings.PasswordUnmatchErrorMessage;
                break;
            case -3:
                messageLabel.Text = Strings.CannotChangePassword;
                break;
        }
    }

    private void AddRules()
    {
        validationManager.AddRule(new ValueRequiredRule(tbOldPassword, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(tbNewPassword, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(tbConfirmNewPassword, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new StringCompareRule(tbNewPassword, tbConfirmNewPassword, Resources.Strings.PasswordUnmatchErrorMessage, Strings.PasswordUnmatchHint));
     
        validationManager.Notifier = new BalloonNotifier();
    }
}
